package definite.Guide.Book.ToolBar;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.TitledBorder;

import definite.Guide.Book.Menu.ShowAction;
import definite.Guide.Book.ToggleButton.DiamondIcon;

public class ToolBarSample {

	private static final int COLOR = 0;
	private static final int STRING = 1;
	static Object[][] buttonColor = { { Color.RED, "RED" },
			{ Color.BLUE, "BLUE" }, { Color.GREEN, "GREEN" },
			{ Color.BLACK, "BLACK" }, null, // separator
			{ Color.CYAN, "CYAN" }, };

	public static class TheAction implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			System.out.println(e.getActionCommand());
		}
	}

	public static void main(String[] args) {
		Runnable runner = new Runnable() {

			@Override
			public void run() {
				JFrame.setDefaultLookAndFeelDecorated(true);
				JFrame frame = new JFrame("ToolBar Sample");
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				ActionListener listener = new TheAction();
				JToolBar toolBar = new JToolBar();
				toolBar.setRollover(true);

				for (Object[] color : buttonColor)
					if (color == null)
						toolBar.addSeparator();
					else {
						Icon icon = new DiamondIcon((Color) color[COLOR], true,
								20, 20);
						JButton button = new JButton(icon);
						button.setActionCommand((String) color[STRING]);
						button.addActionListener(listener);
						toolBar.add(button);
					}

				Action action = new ShowAction(frame);
				JButton btn = new JButton(action);
				toolBar.add(btn);

				JPanel contentPane = (JPanel) frame.getContentPane();
				TitledBorder border = new TitledBorder(null, "ToolBar Sample",
						TitledBorder.TOP, TitledBorder.CENTER, new Font(
								"San-Serif", Font.BOLD, 14), new Color(
								new Random().nextInt(255),
								new Random().nextInt(255),
								new Random().nextInt(255)));
				contentPane.setBorder(border);

				contentPane.add(toolBar, BorderLayout.NORTH);

				JTextArea text = new JTextArea();
				text.setEditable(true);
				text.setSelectionColor(Color.red);
				text.setLineWrap(true);
				text.setWrapStyleWord(true);
				text.setColumns(5);
				text.setRows(5);

				JScrollPane crollPane = new JScrollPane(text);
				crollPane
						.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
				crollPane
						.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
				contentPane.add(crollPane, BorderLayout.CENTER);
				frame.setLocationRelativeTo(null);
				frame.setSize(350, 150);
				frame.setVisible(true);
			}
		};

		EventQueue.invokeLater(runner);
	}
}
